package com.oss.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.oss.client.dto.OssSecurityResponse;
import org.oss.util.Md5Util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.utils.CodingUtils;
import com.aliyun.oss.model.PutObjectResult;

public class OSSUploadService {
	

	public PutObjectResult putBySTS(OssSecurityResponse security,InputStream input, String fileName) throws IOException{
    	CodingUtils.assertParameterNotNull(security, "security info");
    	String accessKeyId =security.getAccessKeyId();
		String accessKeySecret =  security.getAccessKeySecret();
		String securityToken =  security.getSecurityToken();
		String key = security.getKey();
		String endpoint = security.getEndpoint();
		String bucketName = security.getBucketName();
		OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret, securityToken);
		PutObjectResult result = client.putObject(bucketName, key+security.getFileId()+"-"+fileName, input);
		return result;
	}
    
    public PutObjectResult putBySTS(OssSecurityResponse security,File f, String fileName) throws IOException{
    	CodingUtils.assertParameterNotNull(security, "security info");
    	String accessKeyId =security.getAccessKeyId();
		String accessKeySecret =  security.getAccessKeySecret();
		String securityToken =  security.getSecurityToken();
		String key = security.getKey();
		String endpoint = security.getEndpoint();
		String bucketName = security.getBucketName();
		OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret, securityToken);
		System.out.println(key);
		PutObjectResult result = client.putObject(bucketName, key+security.getFileId()+"-"+f.getName(), f);
		System.out.println(Md5Util.getMd5ByFile(f));
		System.out.println(result.getETag());
		
		return result;
	}
    
    
    
}
